package com.code.wanmi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.code.wanmi.entity.wm_user.WmSysMenu;
import com.code.wanmi.entity.wm_user.WmSysRole;
import com.code.wanmi.entity.wm_user.WmSysUser;
import com.code.wanmi.util.DataSource;
import org.apache.ibatis.annotations.*;


import java.util.List;
import java.util.Set;


/**
 * <p>
 * 角色信息表 Mapper 接口
 * </p>
 *
 * @author 谢晓峰
 * @since 2019-12-05
 */
@Mapper
public interface WmSysRoleMapper extends BaseMapper<WmSysRole> {

 @DataSource("wmuser")
    @Insert({
            "INSERT INTO wm_sys_role(role_name,role_key) VALUES(#{roleName},#{roleKey})"
    })
 @Results({
         @Result(column = "role_name", property = "roleName"),
         @Result(column = "role_key", property = "roleKey")
 })

   Integer addRoletwo(WmSysRole wmSysRole);

 @DataSource("wmuser")
    @Insert({
            "INSERT INTO wm_sys_user_role VALUES(#{u.userId},#{r.roleId})"
    })
    Integer insertRole(WmSysRole r, WmSysUser u);

 @DataSource("wmuser")
    @Update({"UPDATE wm_sys_user_role SET role_id=#{role.roleId} WHERE user_id=#{user.userId} AND role_id = #{role.roleId}"})
 @Results({

         @Result(column = "role_id", property = "roleId")
 })
    Integer updateUserRole(WmSysUser user, WmSysRole role);


    //    通过角色名查询角色信息
    @DataSource("wmuser")
    @Select("SELECT\n" +
            "wm_user.wm_sys_role.role_name,\n" +
            "wm_user.wm_sys_role.role_key,\n" +
            "wm_user.wm_sys_role.role_sort,\n" +
            "wm_user.wm_sys_role.`status`,\n" +
            "wm_user.wm_sys_role.create_by,\n" +
            "wm_user.wm_sys_role.create_time,\n" +
            "wm_user.wm_sys_role.update_by,\n" +
            "wm_user.wm_sys_role.update_time,\n" +
            "wm_user.wm_sys_role.remark,\n" +
            "wm_user.wm_sys_role.role_id\n"+
            "wm_user.wm_sys_role.remark FROM wm_user.wm_sys_role where role_name=#{roleName}")
    @Results({
            @Result(column = "role_name", property = "roleName"),
            @Result(column = "role_key", property = "roleKey"),
            @Result(column = "role_sort", property = "roleTypr"),
            @Result(column = "create_by", property = "createBy"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_by", property = "updateBy"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "role_id", property = "roleId")


    })
    WmSysRole findRoleByName(String roleName);

 @DataSource("wmuser")
    @Select({
            "select role_id,role_name,role_key,role_sort,status,create_by,create_by,create_time,update_time,update_by,remark from wm_sys_role where status = 0"
    })
 @Results({
         @Result(column = "role_id", property = "roleId"),
         @Result(column = "role_name", property = "roleName"),
         @Result(column = "role_key", property = "roleKey"),
         @Result(column = "role_sort", property = "roleSort"),
         @Result(column = "create_by", property = "createBy"),
         @Result(column = "create_time", property = "createTime"),
         @Result(column = "update_by", property = "updateBy"),
         @Result(column = "update_time", property = "updateTime")
 })
    List<WmSysRole> getRoles();

 /**通过角色ID查询菜单权限信息*/
 @DataSource("wmuser")
    @Select({
            "SELECT w.* FROM sys_role_menu wm " +
                    "inner join wm_sys_menu w on wm.menu_id = w.menu_id " +
//                    "inner join wm_sys_role r on wm.role_id = r.role_id " +
                    "WHERE role_id=#{role_id} "
    })
 @Results({
         @Result(column = "menu_id",property = "menuId"),
         @Result(column = "menu_name",property = "menuName"),
         @Result(column = "static_code",property = "staticCode"),
         @Result(column = "parent_code",property = "parentCode"),
         @Result(column = "order_num",property = "orderNum")
 })
    List<WmSysMenu> getRoleById(long role_id);

    /**根据角色ID删除角色*/
    @DataSource("wmuser")
    @Update({"UPDATE wm_sys_role SET status = 1 WHERE role_id=#{roleId}"})
    @Results({
            @Result(column = "role_id", property = "roleId")
    })

    Integer deleteRoleById(Long roleId);

    /**根据用户ID查询用户角色名
     * @param uid*/
    @DataSource("wmuser")
 @Select({
         "select w.role_id,role_name from wm_sys_user_role wu inner join wm_sys_role w on wu.role_id = w.role_id where user_id = #{uid} "
 })
    @Results({
            @Result(column = "role_id",property = "roleId"),
            @Result(column = "role_name",property = "roleName"),
            @Result(column = "user_id",property = "userId"),
    })
 Set<WmSysRole> getRoleNameByUserId(Long uid);


}
